<?php
/**
 * Created by PhpStorm.
 * User: ly
 * Date: 2018\3\16 0016
 * Time: 15:18
 */
namespace backend\controllers;
use Yii;
use yii\base\Exception;
use common\models\Responses;
use yii\db\Query;

class LockController extends BaseController
{
    public function actionUnLock()
    {
        /*
         * 当用户登录后要解锁所有解锁内容即编辑解锁
         * @userID
         * package
            package_initiation
            package_initiation_income
            package_initiation_plan
            package_transfer
            project
            project_investigation
            project_investigation_lawsuit
            project_investigation_stakeholder
            project_payment_collection
            project_pre_evaluation_interview
            project_pre_evaluation_lawsuit
            project_pre_evaluation_mortgage
            project_pre_evaluation_other_note
            project_pre_evaluation_other_value
            project_pre_evaluation_seal
            project_pre_evaluation_value
            project_pre_evaluation_value_evaluation
            project_team
            project_transfer
            project_transfer_history
         * */
        if(Yii::$app->request->isGet){
            $userID=intval(Yii::$app->request->get('userID',0));
            if($userID>0){
                $dbList=['package','package_initiation','package_initiation_income','package_initiation_plan','package_transfer',
                    'project','project_investigation','project_investigation_lawsuit','project_investigation_stakeholder','project_payment_collection',
                    'project_pre_evaluation_interview','project_pre_evaluation_lawsuit','project_pre_evaluation_mortgage','project_pre_evaluation_other_note',
                    'project_pre_evaluation_other_value','project_pre_evaluation_seal','project_pre_evaluation_value','project_pre_evaluation_value_evaluation',
                    'project_team','project_transfer','project_transfer_history'];
                //TODO 操作记录
                $transaction = Yii::$app->db->beginTransaction();
                try {
                    foreach ($dbList as $dbName){
                        $changeData = [
                            'lock_user' => '',
                            'lock_timestamp' => ''
                        ];
                        $lockArray=(new Query())->from($dbName)->where("lock_user=$userID")->count();
                        if($lockArray > 0){
                            $res = Yii::$app->db->createCommand()->update($dbName, $changeData, "lock_user=$userID")->execute();
                            if (!$res) {
                                throw new Exception('解锁有误！');
                            }
                        }
                    }
                    $transaction->commit();
                    return json_encode(Responses::response200('', 0, '解锁成功'));
                } catch (\Exception $e) {
                    $transaction->rollBack();
                    return json_encode(Responses::response404($e->getMessage()));
                } catch (\Throwable $e) {
                    $transaction->rollBack();
                    return json_encode(Responses::response404($e->getMessage()));
                }
            }

        }
        return new Exception('访问有误！');
    }
}